package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsInspectTaskDetailMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsInspectTaskDetailMapperImpl;
import cc.rengu.igas.bsps.common.entity.InspectTaskDetail;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.BankManagerService;
import cc.rengu.igas.bsps.facade.bean.InspectMchntStatisInfoBean;
import cc.rengu.igas.bsps.facade.bean.InspectStatisInfoBean;
import cc.rengu.igas.bsps.facade.request.NeedInspectMchntStatisQueryRequest;
import cc.rengu.igas.bsps.facade.response.NeedInspectMchntStatisQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/04/07
 */
public class NeedInspectMchntStatisQueryService extends BankManagerService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        NeedInspectMchntStatisQueryRequest needInspectMchntStatisQueryRequest = new NeedInspectMchntStatisQueryRequest();
        ConvertUtil.convertOutput(needInspectMchntStatisQueryRequest);
        return needInspectMchntStatisQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        NeedInspectMchntStatisQueryRequest needInspectMchntStatisQueryRequest = (NeedInspectMchntStatisQueryRequest) request;
        /*获取请求报文参数：法人行号，用户标识，拓展经理工号，开始日期，截止日期*/
        String instId = needInspectMchntStatisQueryRequest.getHeader().getInstId();
        String userId = needInspectMchntStatisQueryRequest.getUserId();
        String managerId = needInspectMchntStatisQueryRequest.getManagerId();
        String startDate = needInspectMchntStatisQueryRequest.getStartDate();
        String endDate = needInspectMchntStatisQueryRequest.getEndDate();
        /*请求参数必填性校验*/
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(managerId)) {
            rglog.error("巡检商户统计信息请求参数必填校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        startDate = DateUtil.dateStringFormatConvert(startDate, "yyyyMMdd", "yyyy-MM-dd");
        endDate = DateUtil.dateStringFormatConvert(endDate, "yyyyMMdd", "yyyy-MM-dd");
        /*根据拓展经理工号查询拓展经理待巡检商户统计数据*/
        List<InspectStatisInfoBean> inspectStatisInfoList = new ArrayList<>();
        BspsInspectTaskDetailMapper bspsInspectTaskDetailMapper = new BspsInspectTaskDetailMapperImpl();
        List<InspectTaskDetail> inspectTaskDetailList = bspsInspectTaskDetailMapper.selectBspsStatisInspectDetailByManagerId(instId, managerId, startDate, endDate);
        if (!CollectionUtils.isEmpty(inspectTaskDetailList)) {
            inspectTaskDetailList.forEach(item -> convInspectStatisInfo(item, inspectStatisInfoList));
        }
        /*响应报文信息组装*/
        NeedInspectMchntStatisQueryResponse needInspectMchntStatisQueryResponse = new NeedInspectMchntStatisQueryResponse();
        needInspectMchntStatisQueryResponse.setInspectStatisInfoList(inspectStatisInfoList);
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<NeedInspectMchntStatisQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(needInspectMchntStatisQueryResponse);
        return bizResponse;
    }

    /*待巡检商户信息转换
     * inspectTaskDetail 待巡检商户信息
     * inspectStatisInfoList 待巡检商户信息统计列表
     */
    private void convInspectStatisInfo(InspectTaskDetail inspectTaskDetail, List<InspectStatisInfoBean> inspectStatisInfoList) {
        if (inspectStatisInfoList.isEmpty()) {
            InspectStatisInfoBean inspectStatisInfo = new InspectStatisInfoBean();
            /*巡检方式赋值*/
            inspectStatisInfo.setInspectType(inspectTaskDetail.getInspectType());
            List<InspectMchntStatisInfoBean> inspectMchntStatisInfoList = new ArrayList<>();
            InspectMchntStatisInfoBean inspectMchntStatisInfo = new InspectMchntStatisInfoBean();
            /*巡检状态赋值*/
            inspectMchntStatisInfo.setInspectStatus(inspectTaskDetail.getInspectStatus());
            /*待巡检数量统计*/
            inspectMchntStatisInfo.setInspectMchntNum(inspectTaskDetail.getRemark5());
            /*统计列表添加对象*/
            inspectMchntStatisInfoList.add(inspectMchntStatisInfo);
            inspectStatisInfo.setInspectMchntStatisInfoList(inspectMchntStatisInfoList);
            inspectStatisInfoList.add(inspectStatisInfo);
        } else {
            Optional<InspectStatisInfoBean> inspectStatisInfoOpt = inspectStatisInfoList.stream().filter(item -> item.getInspectType().equals(inspectTaskDetail.getInspectType())).findFirst();
            if (inspectStatisInfoOpt.isPresent()) {
                InspectMchntStatisInfoBean inspectMchntStatisInfo = new InspectMchntStatisInfoBean();
                /*巡检状态赋值*/
                inspectMchntStatisInfo.setInspectStatus(inspectTaskDetail.getInspectStatus());
                /*待巡检数量统计*/
                inspectMchntStatisInfo.setInspectMchntNum(inspectTaskDetail.getRemark5());
                inspectStatisInfoOpt.get().getInspectMchntStatisInfoList().add(inspectMchntStatisInfo);
            } else {
                InspectStatisInfoBean inspectStatisInfo = new InspectStatisInfoBean();
                /*巡检方式赋值*/
                inspectStatisInfo.setInspectType(inspectTaskDetail.getInspectType());
                List<InspectMchntStatisInfoBean> inspectMchntStatisInfoList = new ArrayList<>();
                InspectMchntStatisInfoBean inspectMchntStatisInfo = new InspectMchntStatisInfoBean();
                /*巡检状态赋值*/
                inspectMchntStatisInfo.setInspectStatus(inspectTaskDetail.getInspectStatus());
                /*待巡检数量统计*/
                inspectMchntStatisInfo.setInspectMchntNum(inspectTaskDetail.getRemark5());
                /*统计列表添加对象*/
                inspectMchntStatisInfoList.add(inspectMchntStatisInfo);
                inspectStatisInfo.setInspectMchntStatisInfoList(inspectMchntStatisInfoList);
                inspectStatisInfoList.add(inspectStatisInfo);
            }
        }
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        NeedInspectMchntStatisQueryResponse needInspectMchntStatisQueryResponse = (NeedInspectMchntStatisQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(needInspectMchntStatisQueryResponse);
    }
}
